package com.headline.controller;

import com.headline.pojo.Headline;
import com.headline.service.HeadlineService;
import com.headline.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/headline")
@CrossOrigin
public class HeadlineController {

    @Autowired
    private HeadlineService headlineService;

    // 根据需求，只是提交了title、article、type的内容，没有提交hid，hid通过token传入
    @PostMapping("/publish")
    public Result publish(@RequestBody Headline headline, @RequestHeader String token) {
        Result result = headlineService.publish(headline, token);
        return result;
    }

    @PostMapping("/findHeadlineByHid")
    public Result findHeadlineById(Integer hid) {
        Headline headline = headlineService.getById(hid);
        Map data = new HashMap();
        data.put("headline", headline);
        return Result.ok(data);
    }

    @PostMapping("/update")
    public Result updateHeadline(@RequestBody Headline headline) {
        Result result = headlineService.updateHeadline(headline);
        return result;
    }

    @PostMapping("/removeByHid")
    public Result removeHeadline(Integer hid) {
        headlineService.removeById(hid);
        return Result.ok(null);
    }
}
